package com.ymatou.query.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.trading.api.parameter.GetExportProductInfoBean;
import com.ymatou.query.trading.api.service.GetExportProductInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.common.entitie.ExportProductInfo;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.common.enums.OrderTypeEnum;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取待导出的商品信息 FreeMaker Template自动生成代码 接口未使用
 */
@TestSuite
public class Ts_GetExportProductInfo {

	private static GetExportProductInfoBean gepibean;
	private static GetExportProductInfoCall gepicall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取待导出的商品信息");
	}

	@Before
	public void caseUp() {

		gepibean = new GetExportProductInfoBean();
		gepicall = new GetExportProductInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gepicall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetExportProductInfo_001() {
		Logger.start(true,"获取待导出的商品信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 2;
			gepibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(0);
			// 开始时间
			gepibean.setStartDate(StartDate);
			// 结束时间
			gepibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			// OrderStatesList.add(3);
			gepibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(OrderTypeEnum.Shangou.getValue());
			gepibean.setOrderTypes(OrderTypesList);
			gepibean.setSearchText(null);
			// 卖家ID
			gepibean.setUserId(SellerId);
			// 发送请求
			gepicall.setData(gepibean);
			gepicall.callService();
			Logger.comment("geoicall send:" + gepicall.getEntityString());
			Logger.comment("geoicall response:" + gepicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gepicall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, gepicall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true, gepicall.getExportProductInfoList().size() > 0, "验证结果长度");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetExportProductInfo_002() {
		Logger.start(true,"获取导出系统自动取消订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int oid = getBuyerOrderId();
			new YmtOrdersWapper().updateOrderStatusByOrderId(oid, OrderStatusEnum.SystemCancel.getValue());
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 2;
			gepibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(0);
			// 开始时间
			gepibean.setStartDate(StartDate);
			// 结束时间
			gepibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			OrderStatesList.add(OrderStatusEnum.SystemCancel.getValue());
			gepibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(1);
			OrderTypesList.add(OrderTypeEnum.Shangou.getValue());
			gepibean.setOrderTypes(OrderTypesList);
			gepibean.setSearchText(null);
			// 卖家ID
			gepibean.setUserId(SellerId);
			// 发送请求
			gepicall.setData(gepibean);
			gepicall.callService();
			Logger.comment("geoicall send:" + gepicall.getEntityString());
			Logger.comment("geoicall response:" + gepicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gepicall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, gepicall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true, gepicall.getExportProductInfoList().size() > 0, "验证结果长度");
			ArrayList<ExportProductInfo> exportOrderInfoList = gepicall.getExportProductInfoList();
			for (ExportProductInfo exportOrderInfo : exportOrderInfoList) {
				Logger.verifyEquals(OrderStatusEnum.SystemCancel.getValue(), exportOrderInfo.getOrderState(),
						"验证OrderState");
				Logger.verifyEquals(OrderTypeEnum.Shangou.getValue(), exportOrderInfo.getOrderType(), "验证OrderState");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetExportProductInfo_003() {
		Logger.start(true,"获取导出等待发货订单信息成功");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int oid = getBuyerOrderId();
			new OrderCallService().acceptOrder(oid, 1113);
			new YmtOrdersWapper().updateOrderStatusByOrderId(oid, 17);
			/**
			 * 查询日期类型(1-下单时间 2-支付时间 3-发货时间 4-申请补款时间)
			 */
			int DateType = 2;
			gepibean.setDateType(DateType);
			String StartDate = YMTDateUtil.getBeforeOrNextDay(-2);
			String EndDate = YMTDateUtil.getBeforeOrNextDay(0);
			// 开始时间
			gepibean.setStartDate(StartDate);
			// 结束时间
			gepibean.setEndDate(EndDate);
			// 订单状态
			ArrayList<Integer> OrderStatesList = new ArrayList<Integer>();
			OrderStatesList.add(OrderStatusEnum.SellerAccept.getValue());
			gepibean.setOrderStates(OrderStatesList);
			// 订单类型
			ArrayList<Integer> OrderTypesList = new ArrayList<Integer>();
			// OrderTypesList.add(1);
			gepibean.setOrderTypes(OrderTypesList);
			gepibean.setSearchText(null);
			// 卖家ID
			gepibean.setUserId(SellerId);
			// 发送请求
			gepicall.setData(gepibean);
			gepicall.callService();
			Logger.comment("geoicall send:" + gepicall.getEntityString());
			Logger.comment("geoicall response:" + gepicall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, gepicall.getCode(), "验证Code");
			// Logger.verifyEquals(ErrorCode.SUCCESSMSG, gepicall.getMsg(),
			// "验证Msg");
			Logger.verifyEquals(true, gepicall.getExportProductInfoList().size() > 0, "验证结果长度");
			ArrayList<ExportProductInfo> exportOrderInfoList = gepicall.getExportProductInfoList();
			for (ExportProductInfo exportOrderInfo : exportOrderInfoList) {
				Logger.verifyEquals(OrderStatusEnum.SellerAccept.getValue(), exportOrderInfo.getOrderState(),
						"验证OrderState");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 创建一个支付过的订单
	 * 
	 * @return
	 */
	public static int getBuyerOrderId() {
		int orderId = 0;
		try {
			// 创建订单
			Map map = new OrderCallService().appSaveOrder();		
			orderId = (Integer) map.get("orderId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 买家付款
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
		} catch (Exception e) {
			e.printStackTrace();
			return orderId;
		}
		return orderId;
	}
}